//
//  FindViewController.m
//  FD
//
//  Created by Yasin on 2018/10/10.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "FindViewController.h"
#import "FindCell.h"
//controller
#import "FindHeaderView.h"
#import "CouponDetaiController.h"

//model
#import "FindModel.h"

@interface FindViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
/**表头*/
@property (nonatomic,  strong) FindHeaderView *headerView;

/**轮播图数据*/
@property (nonatomic,  strong) NSMutableArray *lunboTuArr;
/**数据源数据*/
@property (nonatomic,  strong) NSMutableArray *dataArr;


@end

@implementation FindViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
//    [self setupUI]
//    [self requestFindData];
}

- (void)setupUI {
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"发现";
    
    self.tableView.frame = self.view.bounds;
    [self.view addSubview:self.tableView];
    
    self.headerView = [[FindHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 200)];
    self.tableView.tableHeaderView = self.headerView;
    self.tableView.tableFooterView = [UIView new];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    //隐藏导航条
//    [self.navigationController setNavigationBarHidden:YES];
    //显示底部tabar
    
    self.hidesBottomBarWhenPushed = YES;
    self.tabBarController.tabBar.hidden = NO;
    self.hidesBottomBarWhenPushed = NO;
}

- (void)viewWillDisappear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:NO];
}



#pragma mark - 代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FindCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FindCell"];
    
    //取出模型进行赋值
    FindModel *model  = self.dataArr[indexPath.row];
    cell.findModel = model;
    
    __weak typeof(self) weakSelf = self;
    
    cell.clickObtainCouponBlock = ^{
        //调接口
        [weakSelf requestObtainCouponWithModel:model];
    };
    
    return cell;
}

//Cell 高 180
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 160;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //10-19不让跳
//    CouponDetaiController *couponVC = [[CouponDetaiController alloc] init];
//    couponVC.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:couponVC animated:YES];
}





#pragma mark - getter
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        //背景色
        //        _tableView.backgroundColor = [UIColor lightGrayColor];
        //代理
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        //注册cell
        [_tableView registerNib:[UINib nibWithNibName:@"FindCell" bundle:nil] forCellReuseIdentifier:@"FindCell"];
        
    }
    return _tableView;
}
- (NSMutableArray *)lunboTuArr {
    if (!_lunboTuArr) {
        _lunboTuArr = [NSMutableArray array];
    }
    return _lunboTuArr;
}

- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}
#pragma mark - 请求数据
- (void)requestFindData {
    [FDHttpAPi requestFindDataSuccessBlock:^(id responseObjc) {
        
        NSString *event = responseObjc[@"event"];
        if ([event isEqualToString:@"SUCCESS"]) { //成功
            NSDictionary *dataDic = responseObjc[@"data"];
            //轮播图
            NSArray * whirligigList = dataDic[@"whirligigList"];
            for (NSDictionary *dic1 in whirligigList) {
                NSString *thumbnail = [NSString stringWithFormat:@"%@%@",kPicDomain,dic1[@"thumbnail"]];
                [self.lunboTuArr addObject:thumbnail];
            }
        
            //数据源
            NSArray * couponList = dataDic[@"couponList"];
            for (NSDictionary *dic2 in couponList) {
                FindModel *model = [FindModel mj_objectWithKeyValues:dic2];
                [self.dataArr addObject:model];
            }
            
            
            //回到主线程刷新数据
            dispatch_async(dispatch_get_main_queue(), ^{
                self.headerView.lunbotoArr = self.lunboTuArr;
                [self.tableView reloadData];
            });
            
        }else if ([event isEqualToString:@"ERROR"])  { //失败
            [JRToast showWithText:@"describe"];
        }else { //其他
            
        }
        
    } failBlock:^(NSError *error) {
        FDLog(@"%@",[error localizedDescription]);
    }];
}


//领取优惠券
- (void)requestObtainCouponWithModel:(FindModel *)model {

    [FDHttpAPi requestObtainCouponWithId:model.couponId successBlock:^(id responseObjc) {

        NSString *event = responseObjc[@"event"];
        if ([event isEqualToString:@"SUCCESS"]) {
            //重新获取数据并刷新界面
            [self requestFindData];
        }else if ([event isEqualToString:@"ERROR"]) {
            [JRToast showWithText:responseObjc[@"describe"]];
        }
        
    } failBlock:^(NSError *error) {
        FDLog(@"%@",[error localizedDescription]);
    }];
}
@end
